package org.example.c2;

import lombok.extern.slf4j.Slf4j;

/**
 * @author liu-rui
 * @date 2020/6/8 上午9:50
 * @description
 * 通过unsafe设置int类型数组元素值
 * @since
 */
@Slf4j
public class Demo7 {
    public static void main(String[] args) {
        int[] ary = new int[5];
        final int base = MyUnSafe.getUnsafe().arrayBaseOffset(int[].class);
        final int scale = MyUnSafe.getUnsafe().arrayIndexScale(int[].class);

        log.info("base:{} scale:{}", base, scale);

        for (int i = 0; i < ary.length; i++) {
            MyUnSafe.getUnsafe().putIntVolatile(ary, base + scale * i, i + 10);
        }

        for (int i = 0; i < ary.length; i++) {
            log.info("i: {} value:{}", i, ary[i]);
        }
    }
}
